package com.toulouseculture.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.toulouseculture.R;
import com.toulouseculture.bo.Movie;

public class CinemaProgramAdapter extends BaseAdapter {

	List<Movie> mCinemaProgramList = new ArrayList<Movie>();

	Context context;

	LayoutInflater inflater;

	private class ViewHolder {
		TextView tvName;
		TextView tvTimes;
	}

	public CinemaProgramAdapter(List<Movie> cinemaProgramList, Context context) {
		super();
		this.mCinemaProgramList = cinemaProgramList;
		this.context = context;

		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mCinemaProgramList.size();
	}

	@Override
	public Object getItem(int position) {
		return mCinemaProgramList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mCinemaProgramList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {

			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.cinemaprogramrow, null);

			holder.tvName = (TextView) convertView.findViewById(R.id.movieName);
			holder.tvTimes = (TextView) convertView.findViewById(R.id.movieTimes);

			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		Movie movie = mCinemaProgramList.get(position);

		holder.tvName.setText(movie.getMovieName() + (movie.getIs3D() ? " 3D" : "") + (movie.getIsVf() ? " VF" : " VO"));
		holder.tvTimes.setText(formatTimeList(movie.getTimeList()));

		return convertView;

	}

	protected String formatTimeList(HashMap<Date, List<String>> timeList) {

		String timeFormated = "";

		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

		for (Date d : timeList.keySet()) {

			timeFormated = format.format(d) + ": ";

			for (String t : timeList.get(d)) {
				timeFormated += t + " ";
			}

			timeFormated += "\n";
		}

		return timeFormated;
	}
}